<script>
import Fav from './api/fav'
import Cart from './api/cart'
import App from './api/App'
import base64 from './util/base64'
// import User from './api/user'
const favModel = new Fav()
const cartModel = new Cart()
// const userModel = new User()

const app = new App()

function compareVersion(v1, v2) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  const len = Math.max(v1.length, v2.length)

  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }

  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i])
    const num2 = parseInt(v2[i])

    if (num1 > num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }

  return 0
}

// compareVersion('1.11.0', '1.9.9') // 1




export default {
    onLaunch: function() {

    },
		onShow: function() {
      // uni.hideTabBar({
      //   animation: false,
      //   success: () => {
      //
      //   },
      //   fail: () => {
      //
      //   }
      // })
      // 版本更新
      // App端特有的逻辑
      /*#ifdef APP-PLUS*/
      let currentVersion = '1.0.0'
      plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
        currentVersion = widgetInfo.version
        console.log('version', currentVersion)
      })
      // const version = 10001
      app.version().then(response => {
        let { data } = response
        data = base64.parse(data)
        console.log('version', data)
        if (compareVersion(data.version, currentVersion) === 1) {
          uni.showModal({
            title: '版本更新',
            content: '尊敬的用户，我们为您准备了最新的版本更新，内含诸多新功能与用户体验优化。点击立即升级，享受更多精彩和便利。',
            confirmText: '立即更新',
            cancelText: '取消', // 如果是紧急情况需要将取消按钮关闭
            howCancel: false,
            success: res => {
              if (res.confirm) {
                const task = plus.downloader.createDownload(data.download_url, {
                  method: 'GET',
                }, (res, status) => {
                  if (status === 200) {
                    uni.hideLoading()
                    plus.runtime.install(res.filename)
                  }
                })
                uni.showLoading({ title: '下载中' })
                task.start()
              }
            }
          })
        }
      })
      /*#endif*/
    },
		onHide: function() {
			// console.log('App Hide')
		},
    methods: {
    }
	}
</script>

<style lang="scss">
	/*每个页面公共css */
  @import "uview-ui/index.scss";

  // 这个目前没有使用
  .content {
    ::v-deep {
      .img {
        width: 100%;
      }
    }
  }

  .text-hide {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .one-lines {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    line-clamp: 1;
    -webkit-box-orient: vertical;
  }


  .two-lines {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .shadow {
    box-shadow: 0 7.5px 5px #e0e1e1;
  }

  // 图片
  //.image {
  //  image {
  //    display: auto;
  //    width: auto;
  //    height: auto;
  //    max-width: 100%;
  //    max-height: 100%;
  //  }
  //}
</style>
